﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using QuanLyRapChieuPhim.DTO;
using System.ComponentModel;

namespace QuanLyRapChieuPhim.DAO
{
    class VeDAO
    {
        DataProvider dp = new DataProvider();

        public BindingList<VeDTO> GetList()
        {
            DataTable dt = new DataTable();
            BindingList<VeDTO> ds = new BindingList<VeDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Ve", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    VeDTO Ve = new VeDTO();
                    Ve.MaVe = int.Parse(row["maVe"].ToString());
                    Ve.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    Ve.HangGhe = int.Parse(row["hangGhe"].ToString());
                    Ve.DayGhe = int.Parse(row["dayGhe"].ToString());
                    Ve.GiaVe = int.Parse(row["giaVe"].ToString());
                    ds.Add(Ve);
                }
                return ds;
            }
            return null;
        }

        public BindingList<VeDTO> GetList_QL()
        {
            DataTable dt = new DataTable();
            BindingList<VeDTO> ds = new BindingList<VeDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Ve_QL", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    VeDTO Ve = new VeDTO();
                    Ve.DaXoa = int.Parse(row["DaXoa"].ToString());
                    Ve.DayGhe = int.Parse(row["dayGhe"].ToString());
                    Ve.GiaVe = int.Parse(row["giaVe"].ToString());
                    Ve.HangGhe = int.Parse(row["hangGhe"].ToString());
                    Ve.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    Ve.MaVe = int.Parse(row["maVe"].ToString());
                    Ve.TenSuatChieu = row["tenSuat"].ToString();
                    ds.Add(Ve);
                }
                return ds;
            }
            return null;
        }

        public BindingList<VeDTO> GetList_Hang(int _Ma)
        {
            DataTable dt = new DataTable();
            BindingList<VeDTO> ds = new BindingList<VeDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaSuat", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetList_Ve_Hang", sqlParameter);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    VeDTO Ve = new VeDTO();
                    Ve.HangGhe = int.Parse(row["hangGhe"].ToString());
                    ds.Add(Ve);
                }
                return ds;
            }
            return null;
        }

        public BindingList<VeDTO> GetList_Day(int _Ma)
        {
            DataTable dt = new DataTable();
            BindingList<VeDTO> ds = new BindingList<VeDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaSuat", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetList_Ve_Day", sqlParameter);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    VeDTO Ve = new VeDTO();
                    Ve.DayGhe = int.Parse(row["dayGhe"].ToString());
                    ds.Add(Ve);
                }
                return ds;
            }
            return null;
        }

        public int GetGiaVe(int _Ma)
        {
            DataTable dt = new DataTable();
            BindingList<VeDTO> ds = new BindingList<VeDTO>();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@MaPhim", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetGiaVe_Phim", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count > 0)
                    return int.Parse(dt.Rows[0][0].ToString());
                return 0;
            }
            return -1;
        }

        public BindingList<VeDTO> GetList_QL_DaXoa()
        {
            DataTable dt = new DataTable();
            BindingList<VeDTO> ds = new BindingList<VeDTO>();
            if (dp.OpenConnect())
            {
                dt = dp.SqlExcuteQuery("uproc_GetList_Ve_QL_DaXoa", null);
                dp.CloseConnect();
                foreach (DataRow row in dt.Rows)
                {
                    VeDTO Ve = new VeDTO();
                    Ve.DaXoa = int.Parse(row["DaXoa"].ToString());
                    Ve.DayGhe = int.Parse(row["dayGhe"].ToString());
                    Ve.GiaVe = int.Parse(row["giaVe"].ToString());
                    Ve.HangGhe = int.Parse(row["hangGhe"].ToString());
                    Ve.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    Ve.MaVe = int.Parse(row["maVe"].ToString());
                    Ve.TenSuatChieu = row["tenSuat"].ToString();
                    ds.Add(Ve);
                }
                return ds;
            }
            return null;
        }

        public VeDTO GetFromId(int _Ma)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = { new SqlParameter("@Ma", _Ma) };
                dt = dp.SqlExcuteQuery("uproc_GetFromID_Ve", sqlParameter);
                VeDTO Ve = new VeDTO();
                foreach (DataRow row in dt.Rows)
                {
                    Ve.MaVe = int.Parse(row["maVe"].ToString());
                    Ve.MaSuatChieu = int.Parse(row["maSuatChieu"].ToString());
                    Ve.HangGhe = int.Parse(row["hangGhe"].ToString());
                    Ve.DayGhe = int.Parse(row["dayGhe"].ToString());
                    Ve.GiaVe = int.Parse(row["giaVe"].ToString());
                }
                return Ve;
            }
            return null;
        }

        public bool Delete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Delete_Ve", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool SetDaMua(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_SetDaMua_Ve", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public int CheckTrong(int _MaSuat, int _HangGhe, int _DayGhe)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaSuat", _MaSuat) ,
                    new SqlParameter("@HangGhe", _HangGhe) ,
                    new SqlParameter("@DayGhe", _DayGhe) 
                };
                DataTable dt = dp.SqlExcuteQuery("uproc_CheckTrong_Ve", sqlParameter);
                dp.CloseConnect();
                if (dt.Rows.Count > 0)
                    return int.Parse(dt.Rows[0][0].ToString());
                return 0;
            }
            return -1;
        }

        public bool SetDaDat(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_SetDaDat_Ve", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool BoDaDat(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_BoDaDat_Ve", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool UnDelete(int _Ma)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_UnDelete_Ve", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }

        public bool Add(VeDTO _Ve)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@DayGhe", _Ve.DayGhe), 
                    new SqlParameter("@GiaVe", _Ve.GiaVe) ,
                    new SqlParameter("@HangGhe", _Ve.HangGhe), 
                    new SqlParameter("@MaSuatChieu", _Ve.MaSuatChieu)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Add_Ve", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }

        public bool Update(VeDTO _Ve)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaVe", _Ve.MaVe), 
                    new SqlParameter("@DayGhe", _Ve.DayGhe), 
                    new SqlParameter("@GiaVe", _Ve.GiaVe) ,
                    new SqlParameter("@HangGhe", _Ve.HangGhe), 
                    new SqlParameter("@MaSuatChieu", _Ve.MaSuatChieu)
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_Update_Ve", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;

        }


        public int GetIDFromSuatHangDay(int _MaSuat,int _HangGhe,int _DayGhe)
        {
            DataTable dt = new DataTable();
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaSuat", _MaSuat) ,
                    new SqlParameter("@HangGhe", _HangGhe) ,
                    new SqlParameter("@DayGhe", _DayGhe) 
                };
                dt = dp.SqlExcuteQuery("uproc_GetIDFromSuatHangDay_Ve", sqlParameter);
                VeDTO Ve = new VeDTO();
                foreach (DataRow row in dt.Rows)
                {
                    Ve.MaVe = int.Parse(row["maVe"].ToString());
                }
                if (dt.Rows.Count > 0)
                    return int.Parse(dt.Rows[0][0].ToString());
                return 0;
            }
            return -1;
        }

        public bool BanVe(int _Ma, int _MaKhachHang)
        {
            if (dp.OpenConnect())
            {
                SqlParameter[] sqlParameter = 
                { 
                    new SqlParameter("@MaKhachHang", _MaKhachHang), 
                    new SqlParameter("@Ma", _Ma) 
                };
                bool kq = dp.SqlExcuteNonQuery("uproc_BanVe_Ve", sqlParameter);
                dp.CloseConnect();
                return kq;
            }
            return false;
        }
    }
}
